﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class CityController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
#endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<Citys> City = (from c in OTDb.Cities
                                join sc in OTDb.States
                                on c.StateID equals sc.StateID into output
                                from j in output.DefaultIfEmpty()
                                select new Citys
                                {
                                    CityID = c.CityID,
                                    CityCode = c.CityCode,
                                    CityName = c.CityName,
                                    CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                    UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue,
                                    StateID = c.StateID,
                                    StateName = j.StateName
                                }).OrderBy(o => o.CityName).ToList<Citys>();

            if (!string.IsNullOrEmpty(searchString))
                City = City.Where(r => r.CityName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount =City.Count;
            City = City.Skip<Citys>(pgindex * (pageNumber - 1))
                .Take<Citys>(pgindex).ToList<Citys>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = City,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            Citys City = null;
            try
            {
                City = new Citys();
                var Citys = OTDb.States.ToList();
                var category = OTDb.States.ToList();
                var countri = OTDb.Countries.ToList();
                City.States = category;
                City.Country = countri;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(City);
        }
        [HttpPost]
        public string Create(string CityCode, string CityName, int StateID)
        {
            string result = "";
            City City = null;
            try
            {
                if (ModelState.IsValid)
                {
                    City = new City();
                    City.CityCode = CityCode;
                    City.CityName = CityName;
                    City.CreatedDate = DateTime.Now;
                    City.UpdatedDate = DateTime.Now;
                    if (StateID != 0)
                        City.StateID = StateID;
                    OTDb.Cities.Add(City);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.States.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int CityID)
        {
            Citys categories = null;
            try
            {
                var categry = OTDb.Cities.Where(r => r.CityID == CityID).SingleOrDefault();
                if (categry != null)
                {
                    categories = new Citys();
                    categories.CountryID = GetCountryID((int)categry.StateID);
                    categories.CityID = categry.CityID;
                    categories.CityName = categry.CityName;
                    categories.CityCode = categry.CityCode;
                    if (categry.StateID != null)
                        categories.StateID = (int)categry.StateID;
                    var category = OTDb.States.ToList();
                    var countri = OTDb.Countries.ToList();
                    categories.States = category;
                    categories.Country = countri;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(categories);
        }
        
        [HttpPost]
        public string Edit(int CityID, string CityCode, string CityName, int StateID)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                   var City = OTDb.Cities.Where(r => r.CityID == CityID).SingleOrDefault();
                    if (City != null)
                    {
                        City.CityCode = CityCode;
                        City.CityName = CityName;
                        City.UpdatedDate = DateTime.Now;
                        if (StateID != 0)
                            City.StateID = StateID;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    var category = OTDb.States.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int CityID)
        {
            Citys categories = null;
            try
            {
                var categry = OTDb.Cities.Where(r => r.CityID == CityID).SingleOrDefault();
                if (categry != null)
                {
                    categories = new Citys();
                    categories.CityID = categry.CityID;
                    categories.CityName = categry.CityName;
                    categories.CityCode = categry.CityCode;
                    if (categry.StateID != null)
                        categories.StateID = (int)categry.StateID;
                    var category = OTDb.States.ToList();
                    categories.States = category;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(categories);
        }

        [HttpPost]
        public string Delete(int CityID, string IsDelete)
        {
            string result = "";
            try
            {
                var City = OTDb.Cities.Where(r => r.CityID == CityID).SingleOrDefault();
                if (City != null)
                {
                    OTDb.Cities.Remove(City);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region JsonDataCall
        private IList<State> GetState(int CountryID)
        {
            return OTDb.States.Where(m => m.CountryID == CountryID).ToList();
        }
        private int GetCountryID(int stateid)
        {
            int rec;
            var query1 = (from c in OTDb.States
                          orderby c.CountryID ascending
                          where c.StateID == stateid
                          select c).SingleOrDefault();
            rec = (int)query1.CountryID;
            return rec;
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadState(string CountryID)
        {
            var stateList = this.GetState(Convert.ToInt32(CountryID));
            var stateData = stateList.Select(m => new SelectListItem()
            {
                Text = m.StateName,
                Value = m.StateID.ToString(),
            });
            return Json(stateData, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
